using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DNAide.Web.UI.Styles;
using DNAide.Web.UI.Scripts;

public partial class _Templates_MasterPages_Default : System.Web.UI.MasterPage
{

	#region Attributes/Properties
	private SiteMapNode _currentSiteMapNode;
	public SiteMapNode CurrentSiteMapNode
	{
		get
		{
			if (_currentSiteMapNode == null)
			{
				return SiteMap.CurrentNode;
			}
			return _currentSiteMapNode;
		}
		set
		{
			_currentSiteMapNode = value;
		}
	}
	#endregion


	protected void Page_Load(object sender, EventArgs e)
	{
		// Register CSS
		RegisterStyles();

		// Register scripts
		RegisterScripts();

		// Build navigation
		BuildNavigation();
	}


	#region Styles/Scripts Registration

	public event EventHandler RegisteredStyles;
	private void RegisterStyles()
	{
		StyleAide.CurrentContext.RegisterInclude("print.css", "~/_client/styles/print.css", "print");
		StyleAide.CurrentContext.RegisterInclude("screen.css", "~/_client/styles/screen.css");
		StyleAide.CurrentContext.RegisterInclude("ie.css", "~/_client/styles/ie.css", "screen", "lte IE 6");
		StyleAide.CurrentContext.RegisterInclude("ie7.css", "~/_client/styles/ie7.css", "screen", "IE 7");

		// Raise an event for pages using this MasterPage to register their own styles
		if (RegisteredStyles != null)
		{
			RegisteredStyles(this, new EventArgs());
		}
	}


	public event EventHandler RegisteredScripts;
	private void RegisterScripts()
	{
		// Configure ScriptAide to register scripts in the "ClientScriptRegisteredHere" control
		ScriptAide.CurrentContext.ScriptRegisterLocation = ScriptRegisterLocation.CustomControl;
		ScriptAide.CurrentContext.CustomRegisterControl = ClientScriptsRegisteredHere;

		// Register jQuery
		ScriptAide.CurrentContext.RegisterDNAideScriptResource(DNAideScriptResource.JQuery, false);

		// Register the global script and auto compress to global.c.js
		// NB. Will only compress if the original script is modified.
		ScriptAide.CurrentContext.RegisterCompressedInclude("global.js", "~/_client/scripts/global.js", "~/_client/scripts/global.c.js");

		// Raise an event for pages using this MasterPage to register their own scripts
		if (RegisteredScripts != null)
		{
			RegisteredScripts(this, new EventArgs());
		}
	}

	#endregion


	#region Navigation 

	private void BuildNavigation()
	{
		// Build navigation primary
		BuildNavigationPrimary();

		// Build navigation secondary
		BuildSecondaryNavigation();
	}

	private void BuildNavigationPrimary()
	{
		// Home link
		if (SiteMap.RootNode.Key != CurrentSiteMapNode.Key)
		{
			NavigationPrimaryLit.Text = string.Format("<li><a href=\"{0}\">{1}</a></li>", SiteMap.RootNode.Url, SiteMap.RootNode.Title);
		}
		else
		{
			NavigationPrimaryLit.Text = AddNavigationLink(SiteMap.RootNode);
		}
		foreach (SiteMapNode childNode in SiteMap.RootNode.ChildNodes)
		{
			NavigationPrimaryLit.Text += AddNavigationLink(childNode);
		}
		if (!string.IsNullOrEmpty(NavigationPrimaryLit.Text))
		{
			NavigationPrimaryLit.Text = "<ul class=\"clearfix\">" + NavigationPrimaryLit.Text + "</ul>";
		}
	}

	private void BuildSecondaryNavigation()
	{
		SiteMapNode sectionNode = null;
		if (CurrentSiteMapNode.Key == SiteMap.RootNode.Key)
		{
			sectionNode = SiteMap.RootNode;
		}
		else
		{
			foreach (SiteMapNode navigationSecondaryNode in SiteMap.RootNode.ChildNodes)
			{
				if (CurrentSiteMapNode.IsDescendantOf(navigationSecondaryNode) || CurrentSiteMapNode.Key == navigationSecondaryNode.Key)
				{
					sectionNode = navigationSecondaryNode;
					break;
				}
			}
		}
		
		if (sectionNode != null)
		{
			foreach (SiteMapNode childNode in sectionNode.ChildNodes) 
			{
				NavigationSecondaryLit.Text += AddNavigationLink(childNode);
			}
			NavigationSecondaryLit.Text = "<ul>" + NavigationSecondaryLit.Text + "</ul>";
		}
	}

	private string AddNavigationLink(SiteMapNode siteMapNode)
	{
		string cssClass="";
		if (CurrentSiteMapNode.Key == siteMapNode.Key || CurrentSiteMapNode.IsDescendantOf(siteMapNode))
		{
			cssClass = " class=\"current\"";
		}
		return string.Format("<li><a href=\"{0}\"{1}>{2}</a></li>", siteMapNode.Url, cssClass, siteMapNode.Title);
	}

	#endregion


}
